#!/bin/env bash
# 编译安装php7.4.7
# 判断是否为root用户，不是root用户则退出
if [ ${USER} != "root" ]; then
	echo "权限不足"
	exit
else
	echo "欢迎光~~临！"
fi

packages_dir="/usr/local/src/tar_packages"
install_dir="/usr/local/php"
php="php-7.4.7"
php_url="10.10.0.12/packages/php-7.4.7.tar.gz"

# 配置yum源
if [ -f /etc/yum.repos.d/CentOS-Base.repo -a -f /etc/yum.repos.d/epel.repo ]; then
	echo "镜像存在"
else	
	rm -rf /etc/yum.repos.d/*
	curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
	curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
	yum makecache
fi

php_install(){
if [ -d /usr/local/php ]; then
    echo "php已安装"
    exit
else
    echo "开始安装php"
fi

# 删除可能安装过的php
yum -y remove php
rm -rf /usr/local/php /bin/php /usr/local/sbin/php-fpm /usr/local/bin/php /usr/local/bin/pear /usr/local/bin/peardev /usr/local/bin/pecl /usr/local/bin/phar /usr/local/bin/phar.phar /usr/local/bin/php-cgi /usr/local/bin/php-config /usr/local/bin/phpdbg /usr/local/bin/phpize

# 安装依赖
yum -y groupinstall "Development Tools"
yum -y install re2c libxml2-devel openssl-devel curl-devel libjpeg-devel libpng-devel libicu-devel freetype-devel openldap-devel openldap openldap-devel sqlite-devel bzip2-devel oniguruma oniguruma-devel libxslt-devel libzip libzip-devel
yum -y upgrade

# 准备软件包
if [ -f ${packages_dir}/${php}.tar.gz ]; then
    echo "软件包已下载"
else
    wget -P ${packages_dir} ${php_url}
fi

if [ -d ${packages_dir}/../${php} ]; then
    echo "php已编译安装"
else
    echo "开始编译"
fi

id www
if [ $? -ne 0 ]; then
    echo "未创建www用户，现在生一个"
    groupadd www
    useradd www -g www
    id www
fi

tar -zxvf ${packages_dir}/${php}.tar.gz -C ${packages_dir}/..
cd ${packages_dir}/../${php}
./configure --prefix=${install_dir} \
--with-config-file-path=${install_dir}/etc \
--with-fpm-user=www --with-fpm-group=www \
--with-curl \
--with-freetype \
--enable-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-mysqli \
--with-openssl \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-jpeg \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-zlib-dir=/user/local/zlib \
--with-bz2 \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml && make && make install
if [ $? -eq 0 ]; then
    echo "编译安装成功"
else
    echo "安装失败，请查看原因"
    exit
fi


cp -f ${packages_dir}/../${php}/php.ini-production ${install_dir}/etc/php.ini
mv ${install_dir}/etc/php-fpm.conf.default ${install_dir}/etc/php-fpm.conf
mv ${install_dir}/etc/php-fpm.d/www.conf.default ${install_dir}/etc/php-fpm.d/www.conf
chown www.www ${install_dir}

sed -i "s#^;error_log.*#error_log = /usr/local/php/var/log/php-fpm.log#" ${install_dir}/etc/php-fpm.conf
sed -i "s#^;pid.*#pid = /usr/local/php/var/run/php-fpm.pid#" ${install_dir}/etc/php-fpm.conf

echo '$PATH:PATH=/usr/local/php/sbin' >> /etc/profile
echo '$PATH:PATH=/usr/local/php/bin' >> /etc/profile
ln -s ${install_dir}/sbin/php-fpm /usr/bin/php-fpm

# 写入systemd
cp -f ${packages_dir}/../php-7.4.7/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
sed -i "14,54d" /usr/lib/systemd/system/php-fpm.service
systemctl daemon-reload
systemctl start php-fpm
systemctl enable php-fpm
ps -ef | grep php-fpm
}

php_install